home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / prospero / propsero.lha / prospero-beta.4.2e / user / vln.c < prev    next >
C/C++ Source or Header  |  1992-02-16  |  8KB  |  312 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <uw-copyright.h>.
  6.  */
  7.  
  8. #include <uw-copyright.h>
  9. #include <stdio.h>
  10. #include <strings.h>
  11.  
  12. #include <pfs.h>
  13. #include <perrno.h>
  14. #include <pmachine.h>
  15. #include <rdgram.h>
  16.  
  17. int    perrno;
  18. int    pfs_debug = 0;
  19.  
  20. main(argc,argv)
  21.     int        argc;
  22.     char    *argv[];
  23.     {
  24.     char        *progname = argv[0];
  25.     char        *cur_arg;
  26.  
  27.     int        flags = 0;       /* Union, etc.                   */
  28.     int        symbolic = 0;    /* Symbolic option               */
  29.     int        native = 0;      /* Native options                */
  30.     int        message = 0;     /* Read Virt filename from stdin */
  31.     int        nm2notdir = 0;   /* Do not treat nm2 as a dir     */
  32.     int        customize = 0;   /* Customizing current view      */
  33.     int        avsflag = 0;     /* Use active VS (no closure)    */
  34.     char        closure[MAX_VPATH];
  35.  
  36.     char        hst[MAX_VPATH];     /* Host name                     */
  37.     char        nm1[MAX_VPATH];     /* Name of object to be linked   */
  38.     char        nm2[MAX_VPATH];     /* The new name for the object   */
  39.  
  40.     char        extp[MAX_VPATH]; /* Type of external access       */
  41.     char        exst[MAX_VPATH]; /* Type string for ext access    */
  42.  
  43.     char        *nlnm;         /* New link name                 */
  44.     char        *ldir;         /* Dir to get link               */
  45.     VLINK        nl;              /* New link                      */
  46.  
  47.     VDIR_ST        dir_st;
  48.     VDIR        dir = &dir_st;
  49.  
  50.     int        tmp = 0;         /* Temp return value             */
  51.  
  52.     vdir_init(dir);
  53.  
  54.     *closure = '\0';
  55.  
  56.     argc--;argv++;
  57.  
  58.     nl = vlalloc();
  59.  
  60.     while (argc > 0 && **argv == '-') {
  61.         cur_arg = argv[0]+1;
  62.  
  63.         /* If a - by itself, then no more arguments */
  64.         if(!*cur_arg) {
  65.         argc--, argv++;
  66.         goto scandone;
  67.         }
  68.  
  69.         while (*cur_arg) {
  70.         switch (*cur_arg++) {
  71.         
  72.         case 'D':  /* Debug level */
  73.             pfs_debug = 1; /* Default debug level */
  74.             sscanf(cur_arg,"%d",&pfs_debug);
  75.             cur_arg += strspn(cur_arg,"0123456789");
  76.             break;
  77.  
  78.         case 'N':  /* Priority (nice) */
  79.             rdgram_priority = RDGRAM_MAX_PRI; /* Use this if no # */
  80.             sscanf(cur_arg,"%d",&rdgram_priority);
  81.             if(rdgram_priority > RDGRAM_MAX_SPRI) 
  82.             rdgram_priority = RDGRAM_MAX_PRI;
  83.             if(rdgram_priority < RDGRAM_MIN_PRI) 
  84.             rdgram_priority = RDGRAM_MIN_PRI;
  85.             cur_arg += strspn(cur_arg,"-0123456789");
  86.             break;
  87.  
  88.         case 'a':
  89.             avsflag++;
  90.             break;
  91.  
  92.         case 'c': 
  93.             /* If customizing directory, then we do not want to find */
  94.             /* the link that is already in the union linked directory*/
  95.             customize++;
  96.             nm2notdir++;
  97.             break;
  98.  
  99.         case 'e': 
  100.             /* Use the rest of the arg as access method */
  101.             if(*cur_arg) sprintf(exst,"EXTERNAL(%s)",cur_arg);
  102.             else strcpy(exst,"EXTERNAL(AFTP,BINARY)");
  103.             cur_arg += strlen(cur_arg);
  104.             nl->type = stcopyr(exst,nl->type);
  105.             nl->hosttype = stcopyr("INTERNET-D",nl->hosttype);
  106.             /* If external, then the hostname must be provided */
  107.             native++; 
  108.             break;
  109.  
  110.         case 'm':
  111.             message++; 
  112.             break;
  113.  
  114.         case 'n':
  115.             native++;
  116.             break;
  117.  
  118.         case 's':
  119.             nl->type = stcopyr("SYM-LINK",nl->type);
  120.             nl->hosttype = stcopyr("VIRTUAL-SYSTEM",nl->hosttype);
  121.             symbolic++;
  122.             break;
  123.  
  124.         case 'u':
  125.             flags = AVL_UNION;
  126.             break;
  127.         
  128.         default:
  129.             fprintf(stderr,
  130.                 "Usage: vln [-a,-m,-s,-u] [-e,-n host] name1 [name2]\n");
  131.             exit(1);
  132.         }
  133.         }
  134.         argc--, argv++;
  135.     }
  136.     scandone:
  137.  
  138.     /* I must still add support for allowing one to */
  139.     /* specify the host type and id type and the    */
  140.     /* link type with the native option             */
  141.  
  142.     /* if stdin is not a tty and OK to use closure */
  143.     /* then we have to extract closure info        */
  144.     if(!avsflag && !isatty(0)) {
  145.         char    *s;
  146.         s = readheader(stdin,"virtual-system-name:");
  147.         if(!s)  {
  148.         fprintf(stderr,"Can't find Virtual-System-Name.\n");
  149.         exit(1);
  150.         }
  151.         strcpy(closure,s);
  152.  
  153.         /* And if the -m option was specified, read the rest to the */
  154.         /* file looking for te virtual file name.                   */
  155.         if(message) {
  156.         s = readheader(stdin,"virtual-file-name:");
  157.         if(!s) {
  158.             fprintf(stderr,"Can't find Virtual-file-name.\n");
  159.             exit(1);
  160.         }
  161.         if(*s == '/') s++;
  162.         strcat(nm1,s);
  163.  
  164.         }
  165.     }
  166.     /* If message option specified, but no stdin, that's an error */
  167.     else if(message) {
  168.         fprintf(stderr,"vln: -m only works if input is redirected\n");
  169.         exit(1);
  170.     }
  171.  
  172.     /* We need at least 1 argument + 1 for host if a native */
  173.     /* link and less name1 if it is read from stdin         */
  174.     if((argc < 1 + (native ? 1 : 0) - (message ? 1 : 0)) ||
  175.        (argc > 2 + (native ? 1 : 0) - (message ? 1 : 0))) {
  176.         fprintf(stderr,
  177.             "Usage: vln [-a,-m,-s,-u] [-e,-n host] name1 [name2]\n");
  178.         exit(1);
  179.     }
  180.  
  181.     /* If a native link, then first argument is the hostname */
  182.     if(native) {
  183.         strcpy(hst,argv[0]);
  184.         argc--;argv++;
  185.     }
  186.     /* If symbolic, but not native, host name is the virtual system name */
  187.     else if (symbolic) {
  188.         char    *s;
  189.  
  190.         if(*closure) s = closure;
  191.         else s = pget_vsname();
  192.  
  193.         if(s) strcpy(hst,s);
  194.         else {
  195.         fprintf(stderr,"vln: Environment not initialized - source vfsetup.source then run vfsetup");
  196.         exit(1);
  197.         }
  198.  
  199.     }
  200.  
  201.     /* Read the first file name if it is required */
  202.     if(!message) {
  203.         strcpy(nm1,argv[0]);
  204.         argc--;argv++;
  205.     }
  206.  
  207.     /* If non native symlink, and if reltive to working dir, */
  208.     /* prepend working directory                             */
  209.     if(!native && symbolic && (*nm1 != '/')) {
  210.         char    temp[MAX_VPATH];
  211.         char    *wd;
  212.         wd = pget_wd();
  213.         if(!wd) {
  214.         fprintf(stderr,"vln: Environment not initialized - source vfsetup.source then run vfsetup");
  215.         exit(1);
  216.         }
  217.         sprintf(temp,"%s/%s",wd,nm1);
  218.         strcpy(nm1,temp);
  219.     }
  220.     
  221.     /* If normal link and we have a closed namespace from */
  222.     /* stdin, then make nm1 relative to closed namespace  */
  223.     if(!native && !symbolic && *closure) {
  224.         char    temp[MAX_VPATH];
  225.         sprintf(temp,"%s:%s",closure,nm1);
  226.         strcpy(nm1,temp);
  227.     }
  228.  
  229.     /* Assume second file name is a directory.  If null, then */
  230.     /* it is the current working directory.                   */
  231.     if(argc > 0) {
  232.         strcpy(nm2,argv[0]);
  233.         argc--;argv++;
  234.     }
  235.     else strcpy(nm2,"");
  236.  
  237.     /* Assume new link name is last component of nm1 */
  238.     nlnm = rindex(nm1,'/');
  239.     if(!nlnm && !(nlnm = rindex(nm1,':'))) nlnm = nm1;
  240.     else nlnm++;
  241.  
  242.     /* If nm1 was either empty, or ended in / or :, must use last */
  243.     /* component of nm2 as link name.  i.e. nm2 is not the        */
  244.     /* directory                                                  */
  245.     if(! *nlnm) nm2notdir++;
  246.  
  247.     /* If a symbolic or native, fill in fields */
  248.     if(symbolic || native) {
  249.         nl->host = hst;
  250.         nl->filename = nm1;
  251.     }
  252.     /* Otherwise, resolve nm1                  */
  253.     else {
  254.         vlfree(nl);
  255.         nl = rd_vlink(nm1);
  256.         if(!nl) {
  257.         fprintf(stderr,"vlink: %s not found\n",nm1);
  258.         exit(1);
  259.         }
  260.     }
  261.     /* Fix type field if nl is a directory */
  262.     if((strcmp(nl->type,"FILE") == 0)  &&
  263.        (get_vdir(nl->host,nl->filename,NULL,dir,GVD_VERIFY,NULL,NULL) == 
  264.         PSUCCESS))
  265.         nl->type = stcopyr("DIRECTORY",nl->type);
  266.          
  267.  
  268.     perrno = 0; *p_err_string = '\0';
  269.     pwarn = 0;  *p_warn_string = '\0';
  270.  
  271.     /* If nm2 might be the name of the directory to use */
  272.     if(!nm2notdir) tmp = add_vlink(nm2,nlnm,nl,flags);
  273.  
  274.     /* If not a directory, then the last component on nm2 is */
  275.     /* the link name, and what precedes it is the dir name   */
  276.     if (nm2notdir || (tmp == DIRSRV_NOT_DIRECTORY)) {
  277.         nlnm = rindex(nm2,'/');
  278.         if(!nlnm) {
  279.         /* If nm2 is null, and the customize option was      */
  280.         /* specified, then nm2 is the last component of nm1  */
  281.         if(customize && (! *nm2)) {
  282.             nlnm = rindex(nm1,'/');
  283.             if(!nlnm) nlnm = nm1;
  284.         }
  285.         else nlnm = nm2;
  286.         ldir = "";
  287.         }
  288.         else {
  289.         if(! *(nlnm+1)) {
  290.             fprintf(stderr,"vln: invalid name %s\n",nm2);
  291.             exit(1);
  292.         }
  293.         *(nlnm++) = '\0';
  294.         ldir = nm2;
  295.         }
  296.  
  297.         /* Add it, this time in the parent directory */
  298.         tmp = add_vlink(ldir,nlnm,nl,flags);
  299.     }
  300.  
  301.     if(tmp) {
  302.         fprintf(stderr,"%s",progname);
  303.         perrmesg(" failed: ", tmp, NULL);
  304.         exit(1);
  305.     }
  306.  
  307.     if(pwarn) pwarnmesg("WARNING: ",0,NULL);
  308.  
  309.     exit(0);
  310.     }
  311.  
  312.